<?php

class SiteController extends Controller
{
    /**
     * Declares class-based actions.
     */
    public $defaultAction = 'index';
    public $route = '';   

    public function filters()
    {
        return array('accessControl'); 
    }

    public function accessRules()
    {
        return array(
            array('deny',
                'actions'=>array('index', 'logout', 'contact'),
                'users'=>array('?'),
            ),
            array('allow',
                'actions'=>array('index', 'logout','contact'),
                'users'=>array('admin'),
            ),
        );        
    }

    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                    'class'=>'CCaptchaAction',
                    'backColor'=>0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName ==>localhost/leagueofplayers/site/page/view/about
            /*'page'=>array(
                    'class'=>'CViewAction',
            ),*/
        );
    }
    
    public function actionLogin()
    {   
        if(Yii::app()->user->getId()===null){
            $usuario=new UsuariosForm;            
            // Validacion ajax
            if(isset($_POST['ajax']) && $_POST['ajax']==='verticalForm'){
                echo CActiveForm::validate($usuario);
                Yii::app()->end();
            }
            // collect user input data
            if(isset($_POST['UsuariosForm']))
            {
                $usuario->attributes=$_POST['UsuariosForm'];
                // validate user input and redirect to the previous page if valid
                if($usuario->validate() && $usuario->login()){
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
            // display the login form
            $this->layout='none';
            $this->render('login');
        }
        else{
            $this->redirect(array('site/index'));
        }	
    }
    
    public function actionIndex()
    {
        $this->render('index');
    }
    
    public function actionContact()
    {            
        $filtersGrid=new FiltersGrid();
        if (isset($_GET['FiltersGrid'])){
            $filtersGrid->filters=$_GET['FiltersGrid'];
        }

        $dataProvider=Usuarios::model()->getGridData($filtersGrid);   

        if(!isset($_GET['ajax'])) $this->render('contact', array('dataProvider'=>$dataProvider,'filtersGrid'=>$filtersGrid));
        else  $this->renderPartial('contact', array('dataProvider'=>$dataProvider,'filtersGrid'=>$filtersGrid));
    }
    
    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            elseif(Yii::app()->user->getId()===null){
                $this->redirect(array('site/login'));
            }
            else{
                $this->render('error', $error);
            }			
        }
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
}